package org.dromara.netbar.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.netbar.domain.NetbarClient;
import org.dromara.netbar.domain.vo.NetbarClientVo;
import org.dromara.netbar.mapper.NetbarClientMapper;
import org.springframework.stereotype.Service;
import org.dromara.netbar.domain.bo.NetbarSeatDeviceBo;
import org.dromara.netbar.domain.vo.NetbarSeatDeviceVo;
import org.dromara.netbar.domain.NetbarSeatDevice;
import org.dromara.netbar.mapper.NetbarSeatDeviceMapper;
import org.dromara.netbar.service.INetbarSeatDeviceService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 设备管理Service业务层处理
 *
 * @author ZhouWenTao
 * @date 2025-08-18
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class NetbarSeatDeviceServiceImpl implements INetbarSeatDeviceService {

    private final NetbarSeatDeviceMapper baseMapper;
    private final NetbarClientMapper clientMapper;

    /**
     * 查询设备管理
     *
     * @param deviceId 主键
     * @return 设备管理
     */
    @Override
    public NetbarSeatDeviceVo queryById(Long deviceId){
        return baseMapper.selectVoById(deviceId);
    }

    /**
     * 分页查询设备管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 设备管理分页列表
     */
    @Override
    public TableDataInfo<NetbarSeatDeviceVo> queryPageList(NetbarSeatDeviceBo bo, PageQuery pageQuery) {
        List<NetbarClientVo> netbarClientVos = clientMapper.selectVoList(new LambdaQueryWrapper<NetbarClient>().select(NetbarClient::getClientId, NetbarClient::getClientNo));
        List<Long> clientIdList = netbarClientVos.stream().map(NetbarClientVo::getClientId).toList();
        if(CollectionUtil.isEmpty(clientIdList)){
            return TableDataInfo.build(Collections.emptyList());
        }
        bo.setTenantId(LoginHelper.getTenantId());
        Page<NetbarSeatDeviceVo> result = baseMapper.customPageList(pageQuery.build(), bo);

//        LambdaQueryWrapper<NetbarSeatDevice> lqw = buildQueryWrapper(bo);
//        lqw.in(NetbarSeatDevice::getDeviceId, clientIdList);
//        Page<NetbarSeatDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        NetbarSeatDeviceVo netbarSeatDeviceVo = null;
//        for (NetbarClientVo record : netbarClientVos) {
//            netbarSeatDeviceVo = new NetbarSeatDeviceVo();
//
//        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的设备管理列表
     *
     * @param bo 查询条件
     * @return 设备管理列表
     */
    @Override
    public List<NetbarSeatDeviceVo> queryList(NetbarSeatDeviceBo bo) {
        LambdaQueryWrapper<NetbarSeatDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NetbarSeatDevice> buildQueryWrapper(NetbarSeatDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NetbarSeatDevice> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(NetbarSeatDevice::getDeviceId);
        lqw.eq(bo.getSeatTypeId() != null, NetbarSeatDevice::getSeatTypeId, bo.getSeatTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getCpuInfo()), NetbarSeatDevice::getCpuInfo, bo.getCpuInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getGpuInfo()), NetbarSeatDevice::getGpuInfo, bo.getGpuInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getMonitorInfo()), NetbarSeatDevice::getMonitorInfo, bo.getMonitorInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyboardInfo()), NetbarSeatDevice::getKeyboardInfo, bo.getKeyboardInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getMouseInfo()), NetbarSeatDevice::getMouseInfo, bo.getMouseInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getMemoryInfo()), NetbarSeatDevice::getMemoryInfo, bo.getMemoryInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageInfo()), NetbarSeatDevice::getStorageInfo, bo.getStorageInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getMotherboardInfo()), NetbarSeatDevice::getMotherboardInfo, bo.getMotherboardInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getNetworkInfo()), NetbarSeatDevice::getNetworkInfo, bo.getNetworkInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getSoundInfo()), NetbarSeatDevice::getSoundInfo, bo.getSoundInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getOtherInfo()), NetbarSeatDevice::getOtherInfo, bo.getOtherInfo());
        return lqw;
    }

    /**
     * 新增设备管理
     *
     * @param bo 设备管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(NetbarSeatDeviceBo bo) {
        NetbarSeatDevice add = MapstructUtils.convert(bo, NetbarSeatDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDeviceId(add.getDeviceId());
        }
        return flag;
    }

    /**
     * 修改设备管理
     *
     * @param bo 设备管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(NetbarSeatDeviceBo bo) {
        NetbarSeatDevice update = MapstructUtils.convert(bo, NetbarSeatDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.insertOrUpdate(update);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NetbarSeatDevice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除设备管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
